const User = require('./userModel')
const Chapter = require('./chapter')
const Exam = require('./examModel')
const Question = require('./questionModel')
const Paper = require('./paperModel')
const UserActiveExam = require('./userActiveExam')
const ActiveCode = require('./activeCodeModel')
const QuestionFav = require('./questionFavModel')
const ErrorQuestion = require('./errorQuestionModel')
const ExamHistory = require('./examHistoryModel')

Paper.hasMany(Question, { foreignKey: 'paper_id' })
Question.belongsTo(Paper, { foreignKey: 'paper_id' })
QuestionFav.belongsTo(Question, { foreignKey: 'question_id' })
QuestionFav.belongsTo(Paper, { foreignKey: 'paper_id' })
QuestionFav.belongsTo(User, { foreignKey: 'user_id' })
QuestionFav.belongsTo(Exam, { foreignKey: 'exam_id' })
ErrorQuestion.belongsTo(Question, { foreignKey: 'question_id' })
ErrorQuestion.belongsTo(User, { foreignKey: 'user_id' })
ErrorQuestion.belongsTo(Paper, { foreignKey: 'paper_id' })
ErrorQuestion.belongsTo(Exam, { foreignKey: 'exam_id' })
ExamHistory.belongsTo(User, { foreignKey: 'user_id' })
ExamHistory.belongsTo(Paper, { foreignKey: 'paper_id' })
Paper.hasMany(ExamHistory, { foreignKey: 'paper_id' })
UserActiveExam.belongsTo(User, { foreignKey: 'user_id' })
UserActiveExam.belongsTo(Exam, { foreignKey: 'exam_id' })
UserActiveExam.belongsTo(ActiveCode, { foreignKey: 'active_code_id' })
ActiveCode.hasMany(UserActiveExam, { foreignKey: 'active_code_id' })
ActiveCode.belongsTo(Exam, { foreignKey: 'exam_id' })
Exam.belongsTo(Exam, { as: 'ParentExam', foreignKey: 'pid' })
Chapter.belongsTo(Chapter, { as: 'ParentChapter', foreignKey: 'pid' })
Chapter.belongsTo(Exam, { foreignKey: 'exam_id' })
Paper.belongsTo(Exam, { foreignKey: 'exam_id' })
Paper.belongsTo(Chapter, { foreignKey: 'chapter_id' })
// ActiveCode.hasOne(UserActiveExam, { foreignKey: 'active_code_id' })
module.exports = {
  User,
  Chapter,
  Exam,
  Question,
  Paper,
  UserActiveExam,
  ActiveCode,
  QuestionFav,
  ErrorQuestion,
  ExamHistory
}
